package com.hundsun.s3.platform.backstage.service.Impl;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import com.hundsun.s3.common.dto.PageBean;
import com.hundsun.s3.common.util.BaseDao;
import com.hundsun.s3.platform.backstage.common.util.ChartUtils;
import com.hundsun.s3.platform.service.OrderReportService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * com.hundsun.s3.platform.backstage.service.Impl
 *
 * @Description: 报表实现类
 * @Author: yangjun23614
 * @Date: Created in 9:50 2018/2/27
 * V1.0 9:50 2018/2/27
 */
@Service
public class OrderReportServiceImpl extends BaseDao implements OrderReportService {

    static Logger logger = Logger.getLogger(OrderReportServiceImpl.class.getName());

    /**
     * @Description: 结果集总数
     * @param: null
     * @return:  int
     * @Author: yangjun23614
     * @Date: 9:56 2018/2/27
     * V1.0
     */
    @Override
    public int getCount() {
        int[] count = new int[1];
        List<Map<String, String>> list = this.call("400010",null);
        ChartUtils.getValuesInListMap(list,count);
        return count[0];
    }

    /**
     * @Description: 功能号400015 ，根据时间查询工单总数
     * @param:
     * @return:
     * @Author: yangjun23614
     * @Date: 18:25 2018/2/28
     * V1.0
     */
    @Override
    public int getCountByTime(Long server_start_time,Long server_end_time){
        int[] count = new int[1];
        Map<String, Object> params = new HashMap<>();
        params.put("server_start_time",server_start_time);
        params.put("server_end_time",server_end_time);
        List<Map<String, String>> list = this.call("400015",params);
        ChartUtils.getValuesInListMap(list,count);
        return count[0];
    }

    /**
     * @Description: 功能号400111 根据ID时间差总数
     * @param:
     * @return:
     * @Author: yangjun23614
     * @Date: 18:25 2018/2/28
     * V1.0
     */
    @Override
    public int getCountByIdTime(String staff_id,Long server_start_time,Long server_end_time){
        int[] count = new int[1];
        Map<String, Object> params = new HashMap<>();
        params.put("staff_id",staff_id);
        params.put("server_start_time",server_start_time);
        params.put("server_end_time",server_end_time);
        List<Map<String, String>> list = this.call("400111",params);
        ChartUtils.getValuesInListMap(list,count);
        return count[0];
    }

    /**
     * @Description: 根据页数的查询
     * @param: PageBean
     * @return: Map<String,Object>
     * @Author: yangjun23614
     * @Date: 9:57 2018/2/27
     * V1.0 
     */
    @Override
    public Map<String, Object> getOrdersByPage(PageBean pageBean) {
        int count = this.getCount();
        pageBean.setCount(count);
        Map<String,Object> params = new HashMap<>();
        params.put("first_record",pageBean.getStart());
        params.put("last_record",pageBean.getEnd());
        List<Order> list = this.qry("400009",params,Order.class);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("page",pageBean);
        return map;
    }

    /**
     * @Description: 400011 可以不传入时间查询，staff_id必须
     * @param: 
     * @return:  
     * @Author: yangjun23614
     * @Date: 15:09 2018/2/27
     * V1.0 
     */
    @Override
    public Map<String, Object> getOrdersByIdTime(String staffId, long serverStartTime, long serverEndTime,PageBean pageBean) {
        int count = this.getCountByIdTime(staffId,serverStartTime,serverEndTime);
        pageBean.setCount(count);
        Map<String,Object> params = new HashMap<>();
        params.put("first_record",pageBean.getStart());
        params.put("last_record",pageBean.getEnd());
        params.put("staff_id",staffId);
        params.put("server_start_time",serverStartTime);
        params.put("server_end_time",serverEndTime);
        List<Order> list = this.qry("400011",params,Order.class);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("page",pageBean);
        return map;
    }

    /**
     * @Description: 400005 不需要传入staff_id
     * @param:
     * @return:
     * @Author: yangjun23614
     * @Date: 16:16 2018/2/28
     * V1.0
     */
    @Override
    public Map<String, Object> getOrdersByTime(long serverStartTime, long serverEndTime, PageBean pageBean) {
        int count = this.getCountByTime(serverStartTime,serverEndTime);
        pageBean.setCount(count);
        Map<String,Object> params = new HashMap<>();
        params.put("first_record",pageBean.getStart());
        params.put("last_record",pageBean.getEnd());
        params.put("server_start_time",serverStartTime);
        params.put("server_end_time",serverEndTime);
        List<Order> list = this.qry("400005",params,Order.class);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("page",pageBean);
        return map;
    }

    /**
     * @Description: 按照年份获取各月份的工单数量
     * @param: year
     * @return: int[]
     * @Author: yangjun23614
     * @Date: 13:50 2018/3/1
     * V1.0 
     */
    @Override
    public int[] getOrderStackedDataByYear(int year){
        int[] datas = new int[12];
        Map<String,Object> params = new HashMap<>();
        params.put("find_year",year);
        List<Map<String, String>> list = this.call("400017",params);
        if (list.size()!=0) {
            Map<String, String> map = list.get(0);
            for (int i = 1; i<= 12; i++) {
                datas[i-1] = Integer.parseInt(map.get("m"+i));
            }
        }
        return datas;
    }

    /**
     * @Description: 按照月份获取各天的工单数量
     * @param: [int]
     * @return: int[]
     * @Author: yangjun23614
     * @Date: 17:23 2018/3/6
     * V1.0
     */
    @Override
    public int[] getOrderStackedDataByMonth(int yearMonth) {
        int[] datas = new int[31];
        Map<String,Object> params = new HashMap<>();
        params.put("find_year_month",yearMonth);
        List<Map<String, String>> list = this.call("400021",params);
        if (list.size()!=0) {
            Map<String, String> map = list.get(0);
            for (int i = 1; i <= 31; i++) {
                datas[i - 1] = Integer.parseInt(map.get("Day" + i));
            }
        }
        return datas;
    }

    /**
     * @Description: 按照参数 、功能号 获取月各天的工单数量
     * @param: [int]
     * @return: int[]
     * @Author: yangjun23614
     * @Date: 17:23 2018/3/6
     * V1.0
     */
    @Override
    public int[] getOrderStackedDataByParams(Map<String,Object> params,String functionNo) {
        int[] datas = new int[31];
        List<Map<String, String>> list = this.call(functionNo,params);
        if (list.size()!=0) {
            Map<String, String> map = list.get(0);
            for (int i = 1; i <= 31; i++) {
                datas[i - 1] = Integer.parseInt(map.get("Day" + i));
            }
        }
        return datas;
    }

    /**
     * @Description:  获取饼状图信息
     * @param: 
     * @return:
     * @Author: yangjun23614
     * @Date: 13:33 2018/3/2
     * V1.0 
     */
    @Override
    public int[] getFeedBackPieDataByData(Map<String,Object> params,String functionNo){
        int[] datas = new int[5];
        List<Map<String, String>> list = this.call(functionNo,params);
        logger.info(((Map<String, String>)list.get(0)).entrySet().toString());
        if (list.size()!=0) {
            Map<String, String> map = list.get(0);
            for (int i = 0; i< 5; i++) {
                datas[i] = Integer.parseInt(map.get("grade"+i));
            }
        }
        return datas;
    }
}
